package com.gz.service.impl;

import com.gz.dao.ProductMapper;
import com.gz.entity.Product;
import com.gz.service.ProductService;
import com.gz.vo.BaseRS;
import com.gz.vo.ProductLocVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public BaseRS queryAllProducts() {
        List<Product> products = productMapper.queryAll(null);
        return BaseRS.builder().data(products).code("200").msg("查询成功").build();
    }

    @Override
    public BaseRS queryProductsByPrarm(Product product) {
        List<Product> products = productMapper.queryAll(product);
        return BaseRS.builder().data(products).code("200").msg("查询成功").build();
    }

    @Override
    public BaseRS queryProductsByPrarmExr(ProductLocVo product) {
        /** 如果传了定位没穿经纬度的话，参数不规范 */
        if (null != product.getDistance() && StringUtils.isEmpty(product.getUserLatitude())) {
            return BaseRS.builder().data("如果传了距离想根据距离查询商品的话，请传经纬度，否则无法计算距离").code("200").msg("查询失败").build();
        }
        String orderField = product.getOrderField();
        String orderType = product.getOrderType();
        if (null != product.getDistance()) {
            if (StringUtils.isEmpty(orderField)) {
                product.setOrderField("distance");
            } else {
                product.setOrderField(orderField + " " + orderType + ",distance asc");
                product.setOrderType(null);
            }
        }
        List<Product> products = productMapper.queryAllExr(product);
        return BaseRS.builder().data(products).code("200").msg("查询成功").build();
    }

    @Override
    public BaseRS queryProductsByType(ProductLocVo product) {
        /** 如果传了定位没穿经纬度的话，参数不规范 */
        if (null != product.getDistance() && StringUtils.isEmpty(product.getUserLatitude())) {
            return BaseRS.builder().data("如果传了距离想根据距离查询商品的话，请传经纬度，否则无法计算距离").code("200").msg("查询失败").build();
        }
        String orderField = product.getOrderField();
        String orderType = product.getOrderType();
//        if(null==product.getDistance()){
//            product.setDistance(new BigDecimal(15));
//        }
        if (!StringUtils.isEmpty(orderField)) {
            product.setOrderField(orderField + " " + orderType + ",distance asc");
            product.setOrderType(null);
        }
        List<Product> products = productMapper.queryAllExr(product);
        Map<String, List<Product>> typeMap = products.parallelStream()
                .collect(Collectors.groupingBy(a -> StringUtils.isEmpty(a.getProductType()) ? "" : a.getProductType()));
        return BaseRS.builder().data(typeMap).code("200").msg("查询成功").build();
    }

    @Override
    public BaseRS queryProductsByStore(ProductLocVo product) throws InvocationTargetException, IllegalAccessException {
        List<Product> res = new ArrayList<>();

        return BaseRS.builder().data(res).code("200").msg("查询成功").build();
    }

    @Override
    public BaseRS queryProductById(Integer id) {
        Product product = productMapper.queryById(id);
        return BaseRS.builder().data(product).code("200").msg("查询成功").build();
    }

    @Override
    public BaseRS saveProduct(Product product) {
        if(org.apache.commons.lang.StringUtils.isEmpty(product.getStoreId())){
            return BaseRS.builder().data(null).code("400").msg("店铺id不能为空").build();
        }
        if (null == product.getUnitPrice()) {
            return BaseRS.builder().data(null).code("400").msg("单价不能为空").build();
        }
        productMapper.insert(product);
        return BaseRS.builder().data(1).code("200").msg("插入成功").build();
    }

    @Override
    public BaseRS saveProducts(List<Product> products) {
        List<Product> collects = products.stream().filter(a -> null == a.getUnitPrice()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(collects)) {
            return BaseRS.builder().data(null).code("400").msg("集合中单个商品，单价不能为空").build();
        }
        productMapper.insertBatch(products);
        return BaseRS.builder().data(1).code("200").msg("插入成功").build();
    }

    @Override
    public BaseRS updateProduct(Product product) {
        productMapper.update(product);
        return BaseRS.builder().data(1).code("200").msg("更新成功").build();
    }

    @Override
    public BaseRS delProduct(String id) {
        productMapper.deleteById(Integer.parseInt(id));
        return BaseRS.builder().data(1).code("200").msg("删除成功").build();
    }
}
